﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;


namespace Anagramme
{
    /// <summary>
    /// <see cref="System.Diagnostics.TraceSource"/> extensions
    /// </summary>
    public static class TraceHelper
    {
        /// <summary>
        /// Trace methods entering
        /// </summary>
        /// <param name="source"><see cref="System.Diagnostics.TraceSource"/> to use to trace the event</param>
        /// <param name="method">Name of method that has been entered</param>
        [System.Diagnostics.DebuggerStepThrough]
        public static void TraceMethodEntering(this TraceSource source, [CallerMemberName] string method = "")
        {
            string message = string.Format(Anagramme.Common.Resources.TraceStrings.METHOD_ENTERING, method);
            source.TraceEvent(TraceEventType.Start, 1, message);
        }

        /// <summary>
        /// Trace methods leaving
        /// </summary>
        /// <param name="source"><see cref="System.Diagnostics.TraceSource"/> to use to trace the event</param>
        /// <param name="method">Name of method that has been leaved</param>
        [System.Diagnostics.DebuggerStepThrough]
        public static void TraceMethodLeaving(this TraceSource source, [CallerMemberName] string method = "")
        {
            string message = string.Format(Anagramme.Common.Resources.TraceStrings.METHOD_LEAVING, method);
            source.TraceEvent(TraceEventType.Stop, 1, message);
        }

        /// <summary>
        /// Trace a property assignation
        /// </summary>
        /// <param name="source"><see cref="System.Diagnostics.TraceSource"/> to use to trace the event</param>
        /// <param name="value">Assigned value</param>
        /// <param name="propertyName">Concerned property</param>
        [System.Diagnostics.DebuggerStepThrough]
        public static void TraceAssign(this TraceSource source, object value, [CallerMemberName] string propertyName = "")
        {
            string message = string.Format(Anagramme.Common.Resources.TraceStrings.ASSIGNATION, propertyName, value);
            source.TraceEvent(TraceEventType.Verbose, 2, message);
        }

        /// <summary>
        /// Trace the return value of a method
        /// </summary>
        /// <param name="source"><see cref="System.Diagnostics.TraceSource"/> to use to trace the event</param>
        /// <param name="value">Value of the return value</param>
        [System.Diagnostics.DebuggerStepThrough]
        public static void TraceReturnValue(this TraceSource source, object value)
        {
            TraceData(source, "returnValue", value);
        }

        /// <summary>
        /// Trace a field value
        /// </summary>
        /// <param name="source"><see cref="System.Diagnostics.TraceSource"/> to use to trace the event</param>
        /// <param name="name">Name of the field</param>
        /// <param name="value">Value of the field</param>
        [System.Diagnostics.DebuggerStepThrough]
        public static void TraceData(this TraceSource source, string name, object value)
        {
            string message = string.Format(Anagramme.Common.Resources.TraceStrings.DATA, name, value);
            source.TraceData(TraceEventType.Verbose, 3, message);
        }
    }
}
